/*============================================================================*/
/*                     AZOR - EMBEDDED SYSTEMS SOFTWARE                       */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:        periodic_tasks_exec.c
* version:         1.0 
* created_by:      MDRR
* date_created:    July 31 2013 
*=============================================================================*/
/* DESCRIPTION :  This file contains the periodic tasks definition.           */
/*============================================================================*/
/* FUNCTION COMMENT : Add here the user functionality, inside a periodic task */
/* according to the desired execution period.                                 */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 31/jul/2013 |                               | MDRR             */
/* This file is created based on apptask module from the old version of this  */
/* scheduler.                                                                 */
/*============================================================================*/

/* Includes */
/* -------- */
#include "periodic_tasks_exec.h"
#include "periodic_tasks_modules.h"


T_UBYTE	rub_MenuCounter,rub_ExtMenuCounter,rub_SecuencialCounter,rub_ModeReg = 0;
T_UWORD	ruw_RedValue,
		ruw_GreenValue,
		ruw_BlueValue,
		ruw_ExtRedValue,
		ruw_ExtGreenValue,
		ruw_ExtBlueValue;


/**************************************************************
 *  Name                 : periodic_tasks_exec_5tks
 *	ID					 : TASK_5TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_5tks(void)
 {
	 
 }
 
 /**************************************************************
 *  Name                 : periodic_tasks_exec_10tks
 *	ID					 : TASK_10TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_10tks(void)
 {
	 static T_UBYTE lub_ExtDataReceived = 0,
			 	 	lub_ExtReceiveCounter = 0,
			 	 	lub_ExtCmdReceived = 0;
	 if(rub_ModeReg == EXTERNAL){
		 if(UART1_S1 & UART_S1_RDRF_MASK){
			 lub_ExtDataReceived = uart1_getchar();
			 switch(lub_ExtReceiveCounter){
			 case 0://Espera valor de SYNC
				 if(lub_ExtDataReceived == SYNC_CMD){
					 lub_ExtReceiveCounter++;
				 }
				 else{
					 lub_ExtReceiveCounter = 0;
				 }
				 break;
			case 1:
				 lub_ExtCmdReceived = lub_ExtDataReceived;
				 lub_ExtReceiveCounter++;
				 break;
			case 2:
				if(lub_ExtDataReceived == 0x00){
					cmd_exc(lub_ExtCmdReceived);
				}
				lub_ExtReceiveCounter = 0;
				 break;
			 default:
				 lub_ExtReceiveCounter = 0;
				 break;
			 }
		 }
	 }
 }
 /**************************************************************
 *  Name                 : periodic_tasks_exec_20tks
 *	ID					 : TASK_20TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_20tks(void)
 {
	 ruw_RedValue = get_adc(CH_RED);
	 ruw_GreenValue = get_adc(CH_GREEN);
	 ruw_BlueValue = get_adc(CH_BLUE);
	 
	 switch(rub_MenuCounter){
	 case OFF:
		 //Modo Apagado
		 set_pwm(PWM_RED,0x00);
		 set_pwm(PWM_GREEN,0x00);
		 set_pwm(PWM_BLUE,0x00);
		 break;
	 case ANALOG_MODE:
		 //Modo Analagico
		 analog_pwm();
		 break;
	 case SECUENCIAL_MODE:
		 //Modo Secuencial
		 secuencial_pwm(rub_SecuencialCounter);
		 break;
	 default:
		 rub_MenuCounter = OFF;
		 break;
	 }
	 	
 }
 
 /**************************************************************
 *  Name                 : periodic_tasks_exec_50tks
 *	ID					 : TASK_50TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_50tks(void)
 {
	 static T_UBYTE lub_DataReceived = 0;
	 
	 if(UART0_S1 & UART_S1_RDRF_MASK){
		 lub_DataReceived = uart0_getchar();
	 	 if(lub_DataReceived == 'r'){
	 		 PrintState();
	 	 }
	 	 if(lub_DataReceived == 'z'){
	 	 	 rub_MenuCounter = OFF;
	 	 }
	 	 else if(lub_DataReceived == 'x'){
	 		 rub_MenuCounter = ANALOG_MODE;
	 	 } 
	 	 else if(lub_DataReceived == 'c'){
	 		 rub_MenuCounter = SECUENCIAL_MODE;
	 	 } 
	 	 //uart_putchar(lub_DataReceived);	//Print Data Received
	 }
 }
 
 /**************************************************************
 *  Name                 : periodic_tasks_exec_100tks
 *	ID					 : TASK_100TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_100tks(void)
 { 
	 app_button2();
	 app_button1();
		 set_indicator(rub_ModeReg);
	 if(rub_MenuCounter == ANALOG_MODE){
		 PrintState();
	 }
	 static T_UBYTE lub_SendCounter = 0;
	 	 if(rub_ModeReg == EXTERNAL){
	 	 	switch(lub_SendCounter){
	 	 		case SYNC_SEND:
	 	 	 		uart1_putchar(SYNC_CMD);
	 	 	 		lub_SendCounter = CMD_SEND;
	 	 	 		break;
	 	 	 	case CMD_SEND:
	 	 	 		cmd_mng();//Command Managment Function
	 	 	 		lub_SendCounter = CHKSUM_SEND;
	 	 	 		break;
	 	 	 	case CHKSUM_SEND:
	 	 	 		app_chksum();
	 	 	 		lub_SendCounter = SYNC_SEND;
	 	 	 		break;
	 	 	 	default:
	 	 	 		lub_SendCounter = SYNC_SEND;
	 	 	 		break;
	 	 	}
	 	 }
 }
 
 /**************************************************************
 *  Name                 : periodic_tasks_exec_500tks
 *	ID					 : TASK_500TKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_500tks(void)
 {

 }
 
 /**************************************************************
 *  Name                 : periodic_tasks_exec_1Mtks
 *	ID					 : TASK_1MTKS
 *  Description          : Container for functionality that is 
                           executed periodically.
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
 void periodic_tasks_exec_1Mtks(void)
 {
	  
	 if((rub_MenuCounter == SECUENCIAL_MODE)){
		 if(rub_SecuencialCounter < 6){
	 	 rub_SecuencialCounter++;
	 	 
		 }
		 else{
			 rub_SecuencialCounter = 0;
			
		 }
	 }
	 else{
		 rub_SecuencialCounter = 0;
	 }
	 
 }
 
